package edu.fdzc.learn.controller;

import edu.fdzc.learn.common.Result;
import edu.fdzc.learn.entity.Comments;
import edu.fdzc.learn.entity.Discussions;
import edu.fdzc.learn.entity.vo.DisUsVo;
import edu.fdzc.learn.service.DiscussionsService;
import edu.fdzc.learn.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/discussios")
public class DiscussionsController {

    @Autowired
    private DiscussionsService discussionsService;

    @PostMapping("{courseId}/add")
    public Result addDiscussion(@PathVariable int courseId, @RequestBody Discussions discussions) {
        Map<String,Object> map= ThreadLocalUtil.get();
        int id = (int) map.get("id");
        discussions.setUserId(id);
        discussions.setCourseId(courseId);
        discussionsService.insert(discussions);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteDiscussion(@PathVariable int id) {
        discussionsService.delete(id);
        return Result.success();
    }

    @GetMapping("/{courseId}")
    public Result<List<DisUsVo>> getAllDiscussions(@PathVariable int courseId) {
        List<DisUsVo> list =  discussionsService.selectByCouId(courseId);
        return Result.success(list);
    }

}
